﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using XFControls.XFPanels.XFPanelItems;
using System.Drawing;
using XFControls.XFPanels;
using XFControls;
using System.Windows.Forms;
using FBEntities.FBObjects;

namespace XFItemControls.XFPanels.XFPanelItems
{
    public class XFItemInboxMessage : IXFItemReceiveClick
    {
        private int _pictureWidth = 45;
        private int _lineSpace = 5;
        private int _height;
        private FBMail _mail;

        #region IXFItem Members

        public XFPanelBase Parent { get; set; }
        public XFItemState State { get; set; }
        public Bitmap Buffer { get; set; }
        public XFItemStyle Style { get; set; }
        public XFItemStyle StyleSubject { get; set; }
        public XFItemType ItemType { get; set; }

        public XFItemInboxMessage(FBMail mail)
        {
            _height = -1;
            _mail = mail;

            Style = new XFItemStyle()
            {
                TextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                TextColor = Color.Black,
                DashStyleBottom = System.Drawing.Drawing2D.DashStyle.Solid,
                BoarderBottomColor = Color.DarkGray,
                Padding = 5
            };

            StyleSubject = new XFItemStyle()
            {
                TextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                TextColor = Color.Navy,
            };

            ItemType = XFItemType.Clickable;

        }

        public int GetHeight()
        {
            if (_mail == null)
                return _pictureWidth + Style.PaddingTop + Style.PaddingBottom;

            if (_height > 0)
                return _height;


            int width = Screen.PrimaryScreen.WorkingArea.Width;


            if (Parent != null)
            {
                width -= Parent.AlphaBarWidth;

                if (Parent.ScrollbarVisible)
                    width -= 5;
            }

            int currX = (Style.PaddingLeft * 2) + _pictureWidth;
            int currY = Style.PaddingTop;

            using (Bitmap b = new Bitmap(10, 10))
            using (Graphics g = Graphics.FromImage(b))
            {
                width -= _pictureWidth + (Style.PaddingLeft * 2) + Style.PaddingRight;

                drawSenderImage(g, _mail);
                writeFrom(_mail, currX, ref currY, width, g);
                writeTitle(_mail, currX, ref currY, width, g);
                writeMessage(_mail, currX, ref currY, width, g);
                writePostTime(_mail, currX, ref currY, g);
            }

            _height = currY;

            return GetHeight();
        }

        public void ResetHeight()
        {
            _height = -1;
        }

        public void ItemPaint(System.Drawing.Graphics g, int x, int y)
        {
            int width = Screen.PrimaryScreen.WorkingArea.Width;

            XFControlUtils.DrawBackground(Style, g, x, y, width, _height);
            XFControlUtils.DrawBoarders(Style, g, x, y, width, _height);

            if (Parent != null)
            {
                width -= Parent.AlphaBarWidth;

                if (Parent.ScrollbarVisible)
                    width -= 5;
            }

            int currX = x + (Style.PaddingLeft * 2) + _pictureWidth;
            int currY = y + Style.PaddingTop;

            width -= _pictureWidth + (Style.PaddingLeft * 2) + Style.PaddingRight;

            drawSenderImage(g, _mail);
            writeFrom(_mail, currX, ref currY, width, g);
            writeTitle(_mail, currX, ref currY, width, g);
            writeMessage(_mail, currX, ref currY, width, g);
            writePostTime(_mail, currX, ref currY, g);

        }

        public XFItemClickResult GetClickResult()
        {
            return XFItemClickResult.Nothing;
        }

        #endregion

        #region IXFItemReceiveClick Members

        public void GetClick(int x, int y)
        {
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (Buffer != null)
                Buffer.Dispose();
            if (Style != null)
                Style.Dispose();
            if (StyleSubject != null)
                StyleSubject.Dispose();

            Style = null;
            StyleSubject = null;
            Buffer = null;
        }

        #endregion

        private void drawSenderImage(Graphics g, FBMail mail)
        {
            using (SolidBrush b = new SolidBrush(Color.LimeGreen))
            {
                g.FillRectangle(b, Style.PaddingLeft, Style.PaddingTop, _pictureWidth, _pictureWidth);
            }
        }

        private void writeFrom(FBMail mail, int currX, ref int currY, int width, Graphics g)
        {
            int finalY = currY;
            using (SolidBrush b = new SolidBrush(Color.Navy))
            {
                if (mail != null)
                {
                    Font f = new Font(Style.TextFont.Name, Style.TextFont.Size + 2, FontStyle.Bold);

                    string from = XFControlUtils.GetSizedString(width, mail.from.name, f);
                    g.DrawString(from, f, b, currX, currY);

                    var mes = g.MeasureString(from, f);
                    finalY += (int)mes.Height + _lineSpace;
                    currX += (int)mes.Width + _lineSpace;
                    f.Dispose();
                }
            }

            currY = finalY;
        }

        private void writeTitle(FBMail mail, int currX, ref int currY, int width, Graphics g)
        {
            using (SolidBrush b = new SolidBrush(StyleSubject.TextColor))
            {

                if (mail == null || string.IsNullOrEmpty(mail.subject)) { }
                else
                {
                    string txt = XFControlUtils.GetSizedString(width, mail.subject, StyleSubject.TextFont);
                    g.DrawString(txt, StyleSubject.TextFont, b, currX, currY);

                    int mes = (int)g.MeasureString(txt, StyleSubject.TextFont).Height;
                    currY += mes + (_lineSpace * 3);
                }
            }
        }

        private void writeMessage(FBMail mail, int currX, ref int currY, int width, Graphics g)
        {
            using (SolidBrush b = new SolidBrush(Style.TextColor))
            {

                if (mail == null || string.IsNullOrEmpty(mail.message)) { }
                else
                {
                    string message = mail.message.Replace("\n", " ").Replace("\r", " ");
                    message = XFControlUtils.EllipsisWord(Screen.PrimaryScreen.WorkingArea.Width * 2, message, Style.TextFont);
                    string txt = XFControlUtils.GetSizedString(width, message, Style.TextFont);
                    g.DrawString(txt, Style.TextFont, b, currX, currY);

                    int mes = (int)g.MeasureString(txt, Style.TextFont).Height;
                    currY += mes + _lineSpace;
                }
            }
        }

        private void writePostTime(FBMail mail, int currX, ref int currY, Graphics g)
        {
            using (SolidBrush b = new SolidBrush(Color.DarkSlateGray))
            {
                DateTime startTime = DateTime.UtcNow;
                DateTime endTime = mail.updated_time;
                TimeSpan span = startTime.Subtract(endTime);

                string display = "";
                if (span.Days > 0)
                {
                    display = span.Days == 1 ? span.Days + " Day ago" : span.Days + " Days ago";
                }
                else if (span.Hours > 0)
                {
                    display = span.Hours == 1 ? span.Hours + " Hour ago" : span.Hours + " Hours ago";
                }
                else if (span.Minutes > 0)
                {
                    display = span.Minutes == 1 ? span.Minutes + " Minute ago" : span.Minutes + " Minutes ago";
                }
                else if (span.Seconds > 0)
                {
                    display = span.Seconds == 1 ? span.Seconds + " Second ago" : span.Seconds + " Seconds ago";
                }
                else
                {
                    display = "Moments ago";
                }

                Font f = new Font(Style.TextFont.Name, Style.TextFont.Size - 1, FontStyle.Bold);

                g.DrawString(display, f, b, currX, currY);
                currY += _lineSpace + (int)g.MeasureString(display, f).Height;
            }
        }
    }
}
